package com.five.controller;

import com.five.dto.UpdateDto;
import com.five.exception.BusinessException;
import com.five.pojo.Novel;
import com.five.result.Result;
import com.five.service.INovelService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/novel")
public class NovelController {

    @Autowired
    private INovelService novelService;

    /**
     * 查询作者的小说列表
     * @param authorId 作者id
     * @return 小说列表
     */
    @PostMapping("/novels/{authorId}")
    public Result<List<Novel>> queryNovelList(@PathVariable Long authorId){
        try {
            List<Novel> novels = novelService.queryNovelList(authorId);
            return Result.success(novels);
        } catch(BusinessException e){
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("查询小说列表失败");
        }
    }

    /**
     * 创建新书
     *
     */
    @PostMapping("/add")
    public Result<String> createNovel(@RequestBody Novel novel){
        try {
            novelService.add(novel);
            return Result.success("success");
        } catch(BusinessException e){
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("创建小说失败");
        }
    }

    /**
     * 删除小说
     */
    @PostMapping("/delete/{novelId}")
    public Result<String> deleteNovel(@PathVariable("novelId") Long novelId){
        try {
            novelService.delete(novelId);
            return Result.success("success");
        } catch(BusinessException e){
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("删除小说失败");
        }
    }

    /**
     * 更新小说信息（小说本身的信息 如：标题、简介、封面等）
     */
     @PostMapping("/update")
    public Result<String> updateNovel(@RequestBody Novel novel){
        try {
            novelService.update(novel);
            return Result.success("success");
        } catch(BusinessException e){
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("更新小说失败");
        }
    }
     /**
     * 小说状态更新
     */
     @PostMapping("/updateStatus")
    public Result<String> updateNovelStatus(@RequestBody UpdateDto dto){
        try {
            novelService.updateStatus(dto);
            return Result.success("success");
        } catch(BusinessException e){
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("更新小说状态失败");
        }
    }

    /**
     * 查询小说详情
     */
    @PostMapping("/detail/{novelId}")
    public Result<Novel> queryNovelDetail(@PathVariable("novelId") Long novelId){
        try {
            Novel novel = novelService.queryById(novelId);
            return Result.success(novel);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("查询小说详情失败");
        }
    }

    /**
     * 小说总字数更新
     */
     @PostMapping("/updateWordCount/{novelId}")
    public Result<String> updateNovelWordCount(@PathVariable("novelId") Long novelId, @RequestParam("wordCount") Integer wordCount){
        try {
            novelService.updateWordCount(novelId, wordCount);
            return Result.success("success");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("更新小说总字数失败");
        }
    }


}
